package org.wuyichun.announcement.handler;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class MyHandler extends TextWebSocketHandler {

    private static List<WebSocketSession> list = new ArrayList<>();
    // onopen 链接创建以后
    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {

        list.add(session);
    }


    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        System.out.println("afterConnectionClosed。。。。。。。。。。。。。。。。。。。");
        super.afterConnectionClosed(session, status);
    }

    public static void sendMessage(String msg2) throws IOException {
        // 获取到session
        for (WebSocketSession session :list) {
            TextMessage msg = new TextMessage("你有一条新公告请查收"+msg2);
            session.sendMessage(msg);
            // 每一个session.sendMessage
        }

    }


}
